package com.springboot.cloud.sysadmin.organization.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.sysadmin.organization.dao.DictDataMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.DictDataQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.*;
import com.springboot.cloud.sysadmin.organization.service.IDictDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */

@Service
@Slf4j
public class DictDataService extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Override
    public boolean add(DictData dictData) {
        return this.saveOrUpdate(dictData);
    }

    @Override
    public boolean delete(Set<String> ids) {

        return this.removeByIds(ids);
    }

    @Override
    public boolean update(DictData dictData) {
        DictData dictData1 = this.getById(dictData.getId());

        if (ObjectUtil.isNull(dictData1) || dictData1.getId().equals(dictData.getId()) && get(dictData.getId())) {
            return false;
        }
        return this.saveOrUpdate(dictData);
    }

    @Override
    public DictData getDictData(String id) {
        return this.getById(id);
    }

    @Override
    public boolean get(String id) {
        DictData dictData = this.getById(id);
        if (ObjectUtil.isNull(dictData)) {
            return false;
        }
        return true;
    }

    @Override
    public List<DictData> getByDictType(String dictType) {
        return this.list(new QueryWrapper<DictData>().eq("dict_type", dictType).orderByAsc("dict_sort"));
    }

    @Override
    public IPage<DictData> query(Page page, DictDataQueryParam dictDataQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<DictData> queryWrapper = dictDataQueryParam.build();
        queryWrapper.like(StrUtil.isNotBlank(dictDataQueryParam.getDictLabel()), "dict_label", dictDataQueryParam.getDictLabel());
        queryWrapper.like(StrUtil.isNotBlank(dictDataQueryParam.getDictValue()), "dict_value", dictDataQueryParam.getDictValue());
        queryWrapper.like(StrUtil.isNotBlank(dictDataQueryParam.getDictType()), "dict_type", dictDataQueryParam.getDictType());
        queryWrapper.eq(StrUtil.isNotBlank(dictDataQueryParam.getStatus()), "status", dictDataQueryParam.getStatus());
        if (dictDataQueryParam.getCreatedTimeStart() != null && dictDataQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(dictDataQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(dictDataQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getAll() {
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DictData::getDictSort, DictData::getDictLabel);
        return this.listMaps(queryWrapper);
    }
}
